<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!CRM_ADM)exit(noauth('只有CRM超级管理员才可以设置哦'));
require_once ZEAI.'sub/ZeaiUP.php';
if($submitok=='add_update' || $submitok=='mod_update'){
	if(empty($title))json_exit(array('flag'=>0,'msg'=>'请输入【门店名称】','focus'=>'username'));
	if(str_len($title)<3)json_exit(array('flag'=>0,'msg'=>'【门店名称】至少要3位长度','focus'=>'username'));
	$bz      = dataIO($bz,'in',1000);
	$title   = dataIO($title,'in',100);
	$sitetitle = dataIO($sitetitle,'in',100);
	$areaid    = dataIO($areaid,'in',100);
	$areatitle = dataIO($areatitle,'in',100);
	$content  = dataIO($content,'in',10000);
	$address  = dataIO($address,'in',200);
	$worktime = dataIO($worktime,'in',100);
	$tel      = dataIO($tel,'in',100);
	$flag=intval($flag);
	$claimnumday=abs(intval($claimnumday));
	$areaid = getareaSQL($areaid);
}
switch ($submitok) {
	case "add_update":
		if(!empty($path_s)){
			adm_pic_reTmpDir_send($path_s,'crm');
			adm_pic_reTmpDir_send(smb($path_s,'b'),'crm');
			$path_s = str_replace('tmp','crm',$path_s);
		}
		if(!empty($ewm)){
			adm_pic_reTmpDir_send($ewm,'crm');
			adm_pic_reTmpDir_send(smb($ewm,'b'),'crm');
			$ewm = str_replace('tmp','crm',$ewm);
		}
		$db->query("INSERT INTO ".__TBL_CRM_AGENT__." (areaid,areatitle,title,sitetitle,content,addtime,bz,flag,claimnumday,address,worktime,tel,path_s,ewm) VALUES ('$areaid','$areatitle','$title','$sitetitle','$content',".ADDTIME.",'$bz',$flag,$claimnumday,'$address','$worktime','$tel','$path_s','$ewm')");
		AddLog('【CRM】->新增门店【'.$title.'】');
		json_exit(array('flag'=>1,'msg'=>'增加成功'));
	break;
	case"mod_update":
		if(!ifint($fid))json_exit(array('flag'=>0,'msg'=>'该门店不存在'));
		$row = $db->ROW(__TBL_CRM_AGENT__,"path_s,ewm","id=".$fid,"num");
		if (!$row)json_exit(array('flag'=>0,'msg'=>'该门店不存在FID'.$fid));
		$data_path_s = $row[0];$data_ewm = $row[1];
		$path_s = tmp_piclist_modupdate($path_s,$data_path_s,'crm','alone');
		$ewm    = tmp_piclist_modupdate($ewm,$data_ewm,'crm','alone');
		$db->query("UPDATE ".__TBL_CRM_AGENT__." SET areaid='$areaid',areatitle='$areatitle',title='$title',sitetitle='$sitetitle',content='$content',bz='$bz',flag=$flag,claimnumday=$claimnumday,address='$address',worktime='$worktime',tel='$tel',path_s='$path_s',ewm='$ewm' WHERE id=".$fid);
		$db->query("UPDATE ".__TBL_USER__." SET agenttitle='$title' WHERE agentid=".$fid);
		AddLog('【CRM】->修改门店【'.$title.'】');
		json_exit(array('flag'=>1,'msg'=>'修改成功'));
	break;
	case"ajax_del":
		if (!ifint($fid))json_exit(array('flag'=>0,'msg'=>'门店不存在或已被删除'));
		$row = $db->ROW(__TBL_CRM_AGENT__,"path_s","id=".$fid);
		if (!$row)json_exit(array('flag'=>0,'msg'=>'该门店不存在FID'.$fid));$path_s = $row[0];
		if(!empty($path_s))@up_send_admindel($path_s.'|'.smb($path_s,'b'));
		$db->query("DELETE FROM ".__TBL_CRM_AGENT__." WHERE id=".$fid);
		AddLog('【CRM】->删除门店【门店ID：'.$fid.'】');
		json_exit(array('flag'=>1,'msg'=>'删除成功'));
	break;
	case"ding":
		if (!ifint($id))alert_adm("forbidden","-1");
		$db->query("UPDATE ".__TBL_CRM_AGENT__." SET px=".ADDTIME." WHERE id=".$id);
		AddLog('【CRM】->置顶门店【门店ID：'.$id.'】');
		header("Location: ".SELF);
	break;
	case"mod":
		if (!ifint($fid))alert_adm("该门店不存在","-1");
		$rt = $db->query("SELECT * FROM ".__TBL_CRM_AGENT__." WHERE id=".$fid);
		if($db->num_rows($rt)){
			$row = $db->fetch_array($rt);
			$title     = dataIO($row['title'],'out');
			$sitetitle = dataIO($row['sitetitle'],'out');
			$uid       = intval($row['uid']);
			$flag      = intval($row['flag']);
			$path_s    = $row['path_s'];
			$ewm       = $row['ewm'];
			$content   = dataIO($row['content'],'out');
			$address   = dataIO($row['address'],'out');
			$worktime  = dataIO($row['worktime'],'out');
			$tel         = dataIO($row['tel'],'out');
			$areaid = dataIO($row['areaid'],'out');
			$areatitle = dataIO($row['areatitle'],'out');
			$bz = dataIO($row['bz'],'out');
			$areaid = explode(',',$areaid);
			$claimnumday = intval($row['claimnumday']);
			$m1 = $areaid[0];$m2 = $areaid[1];$m3 = $areaid[2];$m4 = $areaid[3];
		}else{
			alert_adm("该门店不存在！","-1");
		}
	break;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/res/select4.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/cache/areadata.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.SW{width:131px}
.box{min-width:1100px;padding:10px 0}
</style>
</head>
<body>
<div class="box">
<?php if ($submitok == 'add' || $submitok == 'mod'){?>
    <form id="W_W_W_Z_E__A_I__C_N__FORM">
    <table class="table size2 cols2" style="width:95%;margin:20px auto">
        <tr><td height="50" colspan="2" class="S18 B"><?php echo ($submitok == 'add')?'新增':'修改';?>门店信息</td></tr>
        <tr>
            <td class="tdL">当前状态</td>
            <td class="tdR">
            <input type="checkbox" name="flag" id="flag" class="switch" value="1"<?php echo ($flag == 1)?' checked':'';?>><label for="flag" class="switch-label"><i></i><b>开启</b><b>停止</b></label>　<span class="tips">【开启】后正常使用，【关闭】后冻结该店下面的所有帐号不能登录管理</span></td>
        </tr> 
        <tr>
        <td class="tdL">门店名称</td>
        <td class="tdR"><input id="title" name="title" type="text" class="input W400 size2" maxlength="20" value="<?php echo $title;?>"><span class="tips">如：1号店，万达店等</span></td>
        </tr>
        <tr>
          <td class="tdL">绑定地区显示</td>
          <td class="tdR">
            <script>LevelMenu4('a1|a2|a3|a4||<?php echo $m1; ?>|<?php echo $m2; ?>|<?php echo $m3; ?>|<?php echo $m4; ?>|areaid|areatitle|<?php echo $areatitle;?>','class="size2 SW"');</script>
            <div class="C999 line150 S12">
            选择后，门店后台只能显示此工作地区会员，不限将显示全部地区
            <font class="Cf00">提示：初期客户量少，建议全部选择不限地区，否则客户列表基本是空的或客户不显示的情况</font>
            </div>
          </td>
        </tr>
        
        <tr>
        <td class="tdL">每天认领人数</td>
        <td class="tdR"><input id="claimnumday" name="claimnumday" type="text" class="input W100 size2" maxlength="6" value="<?php echo $claimnumday;?>"><span class="tips">门店员工每天从【公海资源】中认领总人数，填0将不能认领</span></td>
        </tr>
        <tr>
        <td class="tdL">门店LOGO图</td>
        <td class="tdR">
    <div class="picli100" id="picli_path">
        <li class="add" id="path_add"></li>
        <?php if(!empty($path_s)){
            echo '<li><img src="'.$_ZEAI['up2'].'/'.$path_s.'"><i></i></li>';
        }?>
    </div>
        </td>
        </tr>
        <tr>
        <td class="tdL">门店简短介绍</td>
        <td class="tdR"><textarea name="content" rows="5" class="textarea W400 S14" id="content" ><?php echo $content;?></textarea></td>
        </tr>
        <tr>
        <td class="tdL">门店地址</td>
        <td class="tdR"><input id="address" name="address" type="text" class="input W400 size2" value="<?php echo $address;?>" maxlength="100"></td>
        </tr>
        <tr>
        <td class="tdL">营业时间</td>
        <td class="tdR"><input id="worktime" name="worktime" type="text" class="input W400 size2" value="<?php echo $worktime;?>" maxlength="50"><span class="tips">如：早09:30~晚22:00等</span></td>
        </tr>
        <tr>
        <td class="tdL">客服电话</td>
        <td class="tdR"><input id="tel" name="tel" type="text" class="input W400 size2" value="<?php echo $tel;?>" maxlength="50"></td>
        </tr>
        <tr>
        <td class="tdL">客服微信二维码</td>
        <td class="tdR">
            <div class="picli100" id="ewm_path">
                <li class="add" id="ewm_add"></li>
                <?php if(!empty($ewm)){
                    echo '<li><img src="'.$_ZEAI['up2'].'/'.$ewm.'"><i></i></li>';
                }?>
            </div>
        </td>
        </tr>
        <tr>
        <td class="tdL">网站标题</td>
        <td class="tdR"><input id="sitetitle" name="sitetitle" type="text" class="input W400 size2" maxlength="20" value="<?php echo $sitetitle;?>"><span class="tips">如：爱牵线-万达店；打开门店网址后将显示此网站标题</span></td>
        </tr>
        <tr>
            <td class="tdL">备注</td>
            <td class="tdR"><textarea name="bz" rows="3" class="textarea W400 S14" id="bz" placeholder="内部记录，不对外展示"><?php echo $bz;?></textarea></td>
        </tr> 
    </table>
    <input name="path_s" id="path_s" type="hidden" value="" />
    <input name="ewm" id="ewm" type="hidden" value="" />
    <?php if ($submitok == 'mod'){?>
        <input name="submitok" type="hidden" value="mod_update" />
        <input name="fid" type="hidden" value="<?php echo $fid;?>" />
    <?php }else{ ?>
        <input name="submitok" type="hidden" value="add_update" />
    <?php }?>
    </form>
	<?php
}else{?>
    <div class="topsearch">
    	<button type="button" class="btn size2" onClick="zeai.openurl('crm_agent.php?submitok=add')"><i class="ico add">&#xe622;</i> 新增门店</button>
    </div>
    <?php
	$SQL="";
	$Skey = trimm($Skey);
	if (!empty($Skey))$SQL = " AND ( title LIKE '%".dataIO(trimm($Skey),'in')."%' ) ";
	$rt = $db->query("SELECT id,areatitle,title,flag,addtime,claimnumday,path_s,click FROM ".__TBL_CRM_AGENT__." WHERE 1=1 ".$SQL."  ORDER BY px DESC,id DESC LIMIT ".$_ADM['admLimit']);
	$total = $db->num_rows($rt);
	if ($total <= 0 ) {
		echo "<div class='nodataico'><i></i>暂无门店<br><a class='aQINGed' href='javascript:zeai.openurl(\"crm_agent.php?submitok=add\")'>新增门店</a></div>";
	} else {
		$page_skin=2;$pagemode=3;$pagesize=$_ADM['admPageSize'];require_once ZEAI.'sub/page.php';
		?>
		<form id="www_zeai_cn_FORM">
		<table class="tablelist">
		<tr>
		<th width="20"><input type="checkbox" id="checkboxall1" class="checkskin checkboxall"><label for="checkboxall1" class="checkskin-label" style="display:none"><i class="i1"></i></label></th>
		<th width="80" align="center">门店ID</th>
		<th width="39" align="center">置顶</th>
		<th width="60" align="center">门店LOGO</th>
		<th width="200" align="center">门店名称</th>
		<th width="150" align="center">限定地区</th>
		<th width="88" align="center">每天公海认领</th>
		<th width="70" align="center">名下红娘</th>
		<th width="70" align="center">名下客户</th>
		<th width="50" align="center">人气</th>
		<th align="center">&nbsp;</th>
		<th width="75" align="center">创建时间</th>
		<th width="100" align="center">状态</th>
		<th width="170" align="center">操作</th>
		<th width="22" align="center">&nbsp;</th>
		</tr>
		<?php
		for($i=1;$i<=$pagesize;$i++) {
			$rows = $db->fetch_array($rt,'name');
			if(!$rows) break;
			$id      = $rows['id'];
			$path_s  = $rows['path_s'];
			$click   = $rows['click'];
			$addtime = YmdHis($rows['addtime'],'Ymd');
			$title   = dataIO($rows['title'],'out');
			$areatitle   = dataIO($rows['areatitle'],'out');
			$flag        = intval($rows['flag']);
			$claimnumday = intval($rows['claimnumday']);
			//
			if(!empty($Skey))$title = str_replace($Skey,'<font class="Cf00 B">'.$Skey.'</font>',$title);
			$unum1 = $db->COUNT(__TBL_CRM_HN__,"kind='crm' AND agentid=".$id);
			$unum2 = $db->COUNT(__TBL_USER__," kind<>4 AND admid>0 AND agentid=".$id);
			$claimnumday_str = ($claimnumday>0)?'<font class="Cf00">'.$claimnumday.'</font>人':$claimnumday;
			$unum1_str = ($unum1>0)?'<font class="Cf00">'.$unum1.'</font>人':$unum1;
			$unum2_str = ($unum2>0)?'<font class="Cf00">'.$unum2.'</font>人':$unum2;
			$flag_str = ($flag==1)?'<span class="C090"><i class="ico S18">&#xe603;</i><br>正常</span>':'<span class="C999"><i class="ico S18">&#xe692;</i><br>已停止</span>';
			$path_s_url = (!empty($path_s))?$_ZEAI['up2'].'/'.$path_s:HOST.'/res/noP.gif';
			$path_s_str = '<img src="'.$path_s_url.'" class="m yuan">';
		?>
		<tr id="tr<?php echo $id;?>">
		<td width="20" height="40"><input type="checkbox" name="list[]" value="<?php echo $id; ?>" id="id<?php echo $id; ?>" class="checkskin"><label for="id<?php echo $id; ?>" class="checkskin-label"><i class="i1"></i></label></td>
		<td width="80" align="center"><label for="id<?php echo $id; ?>"><?php echo $id;?></label></td>
		<td width="39" align="center"><a href="<?php echo "crm_agent.php?id=".$id; ?>&submitok=ding" class="topico" title="置顶"></a></td>
		<td width="60" align="center">
		  <a href="javascript:;" onClick="parent.piczoom('<?php echo smb($path_s_url,'b'); ?>');" class="zoom"><?php echo $path_s_str; ?></a>
		  </td>
		<td width="200" align="center"><?php echo $title;?></td>
		<td width="150" align="center"><?php echo (!empty($areatitle))?$areatitle:'不限';?></td>
		<td width="88" align="center"><?php echo $claimnumday_str;?></td>
		<td width="70" align="center"><a href="crm_hn.php?agentid=<?php echo $id;?>"class="aHUI"><?php echo $unum1_str; ?></a></td>
		<td width="70" align="center"><a href="crm_user.php?agentid=<?php echo $id;?>"class="aHUI"><?php echo $unum2_str; ?></a></td>
		<td width="50" align="center"><?php echo $click; ?></td>
		<td align="center">&nbsp;</td>
		<td width="75" align="center" class="padding15"><?php echo $addtime;?></td>
		<td width="100" align="center"><?php echo $flag_str;?></td>
		<td width="170" align="center">
            <a class="editbtn" title='修改' clsid="<?php echo $id; ?>"></a>　
            <?php if ($id != 1){?><a clsid="<?php echo $id; ?>" class="delbtn" title='删除'></a><?php }?>
        </td>
		<td width="22" align="center">&nbsp;</td>
		</tr>
		<?php } ?>
		</table>
        <input type="checkbox" id="checkboxall2" class="checkskin checkboxall"><label for="checkboxall2" class="checkskin-label" style="display:none"><i class="i1"></i></label>　
  </form>
		<script>
		var bg = '<?php echo $_Style['list_bg']; ?>',overbg = '<?php echo $_Style['list_overbg']; ?>',selectbg = '<?php echo $_Style['list_selectbg']; ?>';
		zeai.listEach('.editbtn',function(obj){
			obj.onclick = function(){
				var id = parseInt(obj.getAttribute("clsid"));
				zeai.openurl('crm_agent.php?submitok=mod&fid='+id);
			}
		});
		zeai.listEach('.delbtn',function(obj){
			obj.onclick = function(){
				var id = parseInt(obj.getAttribute("clsid"));
				zeai.confirm('★请慎重★　确定真的要删除么？',function(){
					zeai.ajax({url:'crm_agent.php?submitok=ajax_del&fid='+id},function(e){
						rs=zeai.jsoneval(e);
						zeai.msg(0);zeai.msg(rs.msg);
						if(rs.flag==1){setTimeout(function(){location.reload(true);},1000);}
					});
				});
			}
		});
		</script>
		<script src="js/zeai_tablelist.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<?php }}?>
</div>


<?php if ($submitok == 'add' || $submitok == 'mod'){?>
<div class="savebtnbox"><button type="button" id="save" class="btn size3">确定并保存</button></div>
<a href="javascript:zeai.back();" class="ifback ico" title="返回上一页">&#xeaca;</a>
<script>
	var up2='<?php echo $_ZEAI['up2'];?>/',upMaxMB=<?php echo $_UP['upMaxMB']; ?>;
	<?php if($submitok=='mod'){?>
	window.onload=function(){
		path_s_mod();
		function path_s_mod(){
			var i=zeai.tag(picli_path,'i')[0],img=zeai.tag(picli_path,'img')[0];
			if(zeai.empty(i))return;
			path_add.hide();
			var src=img.src.replace(up2,'');
			path_s.value=src;
			i.onclick = function(){
				zeai.confirm('亲~~确认删除么？',function(){
					img.parentNode.remove();path_add.show();path_s.value='';
				});
			}
			img.onclick = function(){parent.piczoom(up2+src.replace('_s.','_b.'));}
		}
		ewm_mod();
		function ewm_mod(){
			var i=zeai.tag(ewm_path,'i')[0],img=zeai.tag(ewm_path,'img')[0];
			if(zeai.empty(i))return;
			ewm_add.hide();
			var src=img.src.replace(up2,'');
			ewm.value=src;
			i.onclick = function(){
				zeai.confirm('亲~~确认删除么？',function(){
					img.parentNode.remove();ewm_add.show();ewm.value='';
				});
			}
			img.onclick = function(){parent.piczoom(up2+src.replace('_s.','_b.'));}
		}
	}
	<?php }?>
	zeai.photoUp({
		btnobj:path_add,
		upMaxMB:upMaxMB,
		url:"crm_agent"+zeai.extname,
		submitok:"ajax_crm_tmp_sb_up",
		end:function(rs){
			zeai.msg(0);zeai.msg(rs.msg);
			if (rs.flag == 1){
				picli_path.append('<li><img src="'+up2+rs.dbname+'"><i></i></li>');
				path_s.value=rs.dbname;
				path_add.hide();
				var i=zeai.tag(o(picli_path),'i')[0],img=zeai.tag(o(picli_path),'img')[0];
				i.onclick = function(){
					zeai.confirm('亲~~确认删除么？',function(){
						img.parentNode.remove();path_add.show();path_s.value='';
					});
				}
				img.onclick = function(){parent.piczoom(up2+rs.dbname.replace('_s.','_b.'));}
			}
		}
	});
	zeai.photoUp({
		btnobj:ewm_add,
		upMaxMB:upMaxMB,
		url:"crm_agent"+zeai.extname,
		submitok:"ajax_crm_tmp_sb_up",
		end:function(rs){
			zeai.msg(0);zeai.msg(rs.msg);
			if (rs.flag == 1){
				ewm_path.append('<li><img src="'+up2+rs.dbname+'"><i></i></li>');
				ewm.value=rs.dbname;
				ewm_add.hide();
				var i=zeai.tag(o(ewm_path),'i')[0],img=zeai.tag(o(ewm_path),'img')[0];
				i.onclick = function(){
					zeai.confirm('亲~~确认删除么？',function(){
						img.parentNode.remove();ewm_add.show();ewm.value='';
					});
				}
				img.onclick = function(){parent.piczoom(up2+rs.dbname.replace('_s.','_b.'));}
			}
		}
	});
	save.onclick = function(){
		var oktips ='<b class="S18">确定保存么？</b><br>保存后，请点击左侧菜单【红娘管理】新增属于此门店下的【红娘主管】，然后把红娘主管帐号密码发给他，他就可以进去创建属于此门店下的其他下属红娘（如：售前，售后，财务等），红娘主管可以给当前门店客户分配售前售后红娘<br><br>下属红娘登录后可以到顶部【红娘CRM】->左侧【客户管理->公海资源】认领用户进行服务，也可以到【客户管理】人工录入客户，录入即在名下';
		zeai.confirm(oktips,function(){
			zeai.ajax({url:'crm_agent'+zeai.extname,form:W_W_W_Z_E__A_I__C_N__FORM},function(e){var rs=zeai.jsoneval(e);
				zeai.msg(0);zeai.msg(rs.msg);
				if (rs.flag == 1){setTimeout(function(){zeai.openurl('crm_agent.php')},1000);}
			});
		});
	}
</script>

<?php }else{ ?>
	<?php if ($total > $pagesize){?>
    <div class="listbottombox" style="text-align:center">
        <input type="checkbox" id="checkboxall2" class="checkskin checkboxall"><label for="checkboxall2" class="checkskin-label" style="display:none"><i class="i1"></i></label>　
        <?php if($total > 0)echo '<div class="pagebox">'.$pagelist.'</div>'; ?>
    </div>
    <?php }?>
<?php } require_once 'bottomadm.php';?>